package com.example.smartcity.ui.act

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.example.smartcity.R
import com.example.smartcity.app.App
import com.example.smartcity.http.SellerDetailResp
import com.example.smartcity.ui.frg.TOFCFrg
import com.example.smartcity.ui.frg.TOFOFrg
import com.example.smartcity.ui.frg.TOFSFrg
import kotlinx.android.synthetic.main.activity_tof_seller_detail.*


class TOFSellerDetailActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_tof_seller_detail)
        var sellerId = intent.extras?.get("seller_id") as Int
        initView(sellerId)
        initData(sellerId)
    }

    private fun initView(sellerId: Int) {
        var frgs = mutableListOf(
            TOFOFrg.newInstance(sellerId),
            TOFCFrg.newInstance(),
            TOFSFrg.newInstance()
        )
        ivAppBarLeft.setOnClickListener { finish() }
        viewPager.adapter = object : FragmentPagerAdapter(
            supportFragmentManager,
            BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
        ) {
            override fun getCount(): Int {
                return frgs.size
            }

            override fun getItem(position: Int): Fragment {
                return frgs[position]
            }

            override fun getPageTitle(position: Int): CharSequence? {
                return when (position) {
                    0 -> "点菜"
                    1 -> "评价"
                    2 -> "商家"
                    else -> {
                        "商家"
                    }
                }
            }
        }
        tabLayout.setupWithViewPager(viewPager, false)
    }

    private fun initData(sellerId: Int) {
        App.fetchData<SellerDetailResp>(
            "http://124.93.196.45:10001/prod-api/api/takeout/seller/$sellerId",
            SellerDetailResp::class.java
        ) {
            var seller = it.data
            Glide.with(this@TOFSellerDetailActivity)
                .load(App.HTTP_BASE_URL + seller.imgUrl)
                .into(ivTop)
            var options = RequestOptions()
            options.transform(CenterCrop(), RoundedCorners(8))
            Glide.with(this@TOFSellerDetailActivity)
                .load(App.HTTP_BASE_URL + seller.imgUrl)
                .apply(options)
                .into(ivRight)
            tvName.text = seller.name
            tv_score.text = "${seller.score}"
            tv_sale_quantity.text = "月销${seller.saleQuantity}"
            tv_avg_cost.text = "人均¥ ${seller.avgCost}"
        }
    }
}